import Vue from 'vue'
import { usePost} from "@/utils/request"
import { URL_link } from '@/constant/url.js'

export const state = () => ({
	currentSessionInfo: {}, // 当前session信息
	currentClientInfo: {}, // 当前聊天客户信息
	currentClientId: '', // 当前聊天客户id
	clientList: [], // 聊天客户列表
	// clientDialogInfoMap: {}, // 聊天客户相关信息映射
	clientDialogList: [], // 客户聊天对话列表
	showClientList: true, // 是否显示客户列表
	scrollDialogMessage: false, // 用户和代理人窗口滚动消息到底部
	scrollGptMessage: false, // gpt和代理人窗口滚动消息到底部
	clientDialogInput: '', // 聊天输入框内容
	gptMessageInfoMap: {}, // 大模型聊天相关信息映射
	gptMessageList: [], // 大模型消息列表
	recognitionModuleInfo: { // 识别模块相关信息
		emotionInfo: {}, // 情绪
		clientTag: [], // 客户标签
		clientIntent: [], // 客户意图
	},
	portraitInfo: { // 客户画像信息
		sesstionTag: [], // 会话标签
		intentionArray: [], // 本次意图
		historyIntentionArray: [], // 历史意图
	},
	customerDemandInfo: {}, // 客户保障检视最新结果
	hasCustomerDemand: false, // 客户是否已投保
	lmPanelWidth: 0, // 大模型面板宽度
})

export const mutations = {
	// 设置当前session信息
	seCurrentSessionInfo(state, data) {
		state.currentSessionInfo = data
	},
	// 设置当前聊天客户信息
	setCurrentClientInfo(state, data) {
		state.currentClientInfo = data
		// 设置当前聊天客户id
		state.currentClientId = data.customer_id
	},
	// 设置客户保障检视最新结果
	setCustomerDemandInfo(state, data) {
		state.customerDemandInfo = data
	},
	// 设置客户是否已投保
	setHasCustomerDemand(state, data) {
		state.hasCustomerDemand = data
	},
	// 设置聊天客户列表
	setClientList(state, data) {
		state.clientList = data
	},
	// 设置聊天客户相关信息映射
	// setClientDialogInfoMap(state, data) {
	// 	if(state.clientDialogInfoMap[data.clientId]) {
	// 		state.clientDialogInfoMap[data.clientId] = data.dialogMessageList
	// 	} else {
	// 		Vue.set(state.clientDialogInfoMap, data.clientId, []); // 使用 Vue.set 来确保属性是响应式的
	// 	}
	// },
	// 设置客户聊天对话列表
	setClientDialogList(state, data) {
		state.clientDialogList = data
	},
	// 切换是否显示客户列表
	toggleShowClientList(state) {
		state.showClientList = !state.showClientList
	},
	// 新增对话消息项
	pushDialogMessage(state, data) {
		// state.clientDialogInfoMap[data.clientId].push(data)
		if(data.clientId === state.currentClientId) {
			state.clientDialogList.push(data)
		}
	},
	// 更新客户聊天信息情绪相关字段
	updateDialogMessageEmotion(state, data) {
		if(data.clientId === state.currentClientId) {
			state.clientDialogList.forEach(ele => {
				if(ele.msgId === data.msgId) {
					ele.emotion = data.emotion
					ele.emotion_item = data.emotion_item
				}
			})
		}
	},	
	// 更新客户聊天最后一条信息
	setClientLastMsgItem(state, data) {
		state.clientList.forEach(element => {
			if(element.customer_id == data.clientId) {
				if(data.isUpdateUnReadCount) {
					element.un_read_count = '0'
				} else {
					if(state.currentClientId !== data.clientId) {
						let un_read_count = element.un_read_count || '0'
						un_read_count++
						element.un_read_count = un_read_count
					}
					element.last_message = data
				}
			}
		})
	},
	// 设置用户和代理人窗口是否滚动消息到底部状态
	setScrollDialogMessage(state, data) {
		state.scrollDialogMessage = data
	},
	// 设置gpt和代理人窗口是否滚动消息到底部状态
	setScrollGptMessage(state, data) {
		state.scrollGptMessage = data
	},
	// 设置聊天输入框内容
	setClientDialogInput(state, data) {
		state.clientDialogInput = data
	},
	// 设置大模型消息列表数据
	setGptMessageList(state, data) {
		state.gptMessageList = data
		// Vue.nextTick(() => {
		// 	state.scrollGptMessage = true
		// })
		setTimeout(() => {
			state.scrollGptMessage = true
		}, 300)
	},
	// 新增大模型消息列表数据
	pushGptMessageList(state, data) {
		let isExistWaitingInfo = false
		state.gptMessageList.forEach(ele => {
			if(ele.msgId === data.msgId && !!data.msgId) {
				ele.message_body_type = data.message_body_type
				ele.message = data.message
				isExistWaitingInfo = true
			}
		})
		if(!isExistWaitingInfo) {
			state.gptMessageList.push(data)
		}
		Vue.nextTick(() => {
			state.scrollGptMessage = true
		})
	},
	// 设置识别模块信息
	setRecognitionModuleInfo(state, data) {
		state.recognitionModuleInfo = data
	},
	// 更新识别模块信息
	updateRecognitionModuleInfo(state, data) {
		state.recognitionModuleInfo[data.moduleName] = data.moduleData
	},
	// 设置客户画像信息
	setPortraitInfo(state, data) {
		state.portraitInfo = data
	},
	// 更新客户画像信息 
	updatePortraitInfo(state, data) {
		state.portraitInfo[data.moduleName] = data.moduleData
		// console.log(state.portraitInfo)
	},
	// 设置大模型面板宽度
	setLmPanelWidth(state, data) {
		state.lmPanelWidth = data
	}
}

export const actions = {
	// 代理人给用户发送消息
	postMsg({ commit, state }, data) {
		usePost(URL_link.postIM, {
			query_tenant_id: LOCAL_CONFIG.query_tenant_id,
			session_uuid: state.currentSessionInfo.session_id,
			client_id: state.currentClientInfo.customer_id,
			client_name: state.currentClientInfo.real_name,
			adviser_id: LOCAL_CONFIG.agent_code,
			adviser_name: LOCAL_CONFIG.agent_name,
			msg_side: data.msg_side, // 1:发送普通消息给客户 0:发送普通消息给代理人
			aicc_msg_type: data.aicc_msg_type,
			aicc_msg_content: data.aicc_msg_content,
			// msg_send_time: nowTime
		}).then(res => {
			let item = {
				message_type:  data.msg_side === '1' ? '2' : '1', // message_type 1.用户->代理人 2.代理人->用户 3.代理人->大模型 4.大模型->代理人
				message_body_type: data.aicc_msg_type,
				create_time: res.data.create_time,
				message: data.aicc_msg_content
			}
			commit('pushDialogMessage', {...item, clientId: state.currentClientId})
			commit('setClientLastMsgItem', {...item, clientId: state.currentClientId})
			Vue.nextTick(() => {
				commit('setScrollDialogMessage', true)
			})
		})
	},
	// 代理人给大模型发送消息
	postGptMsg({ commit, state }, data) {
		let params = {
			adviser_id: LOCAL_CONFIG.agent_code,
			adviser_name: LOCAL_CONFIG.agent_name,
			client_id: state.currentClientInfo.customer_id,
			client_name: state.currentClientInfo.real_name,
			session_uuid: state.currentSessionInfo.session_id,
			...data
		}
		let msgItem = {
			message_type: '3',
			message: {
				content: params.instruction_id ? params.instruction_name : params.quote_msg_content
			}
		}
		commit('pushGptMessageList', msgItem)
		usePost(URL_link.getAdviceInstruction, params).then(res => {
		})
	}
}
